package com.competition.commgr.service.impl;

import java.util.List;

import com.competition.common.exception.ServiceException;
import com.competition.common.utils.StringUtils;
import com.competition.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.competition.commgr.mapper.CompetitionsMapper;
import com.competition.commgr.domain.Competitions;
import com.competition.commgr.service.ICompetitionsService;

import javax.validation.Validator;

/**
 * 赛事管理Service业务层处理
 */
@Service
public class CompetitionsServiceImpl implements ICompetitionsService
{
    private static final Logger log = LoggerFactory.getLogger(CompetitionsServiceImpl.class);

    @Autowired
    protected Validator validator;

    @Autowired
    private CompetitionsMapper competitionsMapper;

    /**
     * 查询赛事管理
     *
     * @param competitionId 赛事管理主键
     * @return 赛事管理
     */
    @Override
    public Competitions selectCompetitionsByCompetitionId(Integer competitionId)
    {
        return competitionsMapper.selectCompetitionsById(competitionId);
    }

    /**
     * 查询赛事管理列表
     *
     * @param competitions 赛事管理
     * @return 赛事管理
     */
    @Override
    public List<Competitions> selectCompetitionsList(Competitions competitions)
    {
        return competitionsMapper.selectCompetitionsList(competitions);
    }

    /**
     * 新增赛事管理
     *
     * @param competitions 赛事管理
     * @return 结果
     */
    @Override
    public int insertCompetitions(Competitions competitions)
    {
        return competitionsMapper.insertCompetitions(competitions);
    }

    /**
     * 修改赛事管理
     *
     * @param competitions 赛事管理
     * @return 结果
     */
    @Override
    public int updateCompetitions(Competitions competitions)
    {
        return competitionsMapper.updateCompetitions(competitions);
    }

    /**
     * 批量删除赛事管理
     *
     * @param competitionIds 需要删除的赛事管理主键
     * @return 结果
     */
    @Override
    public int deleteCompetitionsByCompetitionIds(Integer[] competitionIds)
    {
        return competitionsMapper.deleteCompetitionsByIds(competitionIds);
    }

    /**
     * 删除赛事管理信息
     *
     * @param competitionId 赛事管理主键
     * @return 结果
     */
    @Override
    public int deleteCompetitionsByCompetitionId(Integer competitionId)
    {
        return competitionsMapper.deleteCompetitionsById(competitionId);
    }

    /**
     * 导入赛事数据
     */
    @Override
    public String importCompetitions(List<Competitions> competitionList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(competitionList) || competitionList.size() == 0)
        {
            throw new ServiceException("导入赛事数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (Competitions competitions : competitionList)
        {
            try
            {
                // 验证是否存在这个赛事
                Competitions existingCompetition = competitionsMapper.selectCompetitionsByCompetitionName(competitions.getCompetitionName());
                if (existingCompetition == null)
                {
                    BeanValidators.validateWithException(validator, competitions);
                    competitions.setCreateBy(operName);
                    competitionsMapper.insertCompetitions(competitions);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、赛事 " + competitions.getCompetitionName() + " 导入成功");
                }
                else if (updateSupport)
                {
                    // 更新已存在的赛事
                    BeanValidators.validateWithException(validator, competitions);
                    competitions.setCompetitionId(existingCompetition.getCompetitionId());
                    competitions.setUpdateBy(operName);
                    competitionsMapper.updateCompetitions(competitions);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、赛事 " + competitions.getCompetitionName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、导入失败：" + competitions.getCompetitionName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、导入失败：" + competitions.getCompetitionName();
                failureMsg.append(msg + "，错误信息：" + e.getMessage());
                log.error(msg, e);
            }
        }

        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }

        return successMsg.toString();
    }

    @Override
    public Competitions selectCompetitionsByCompetitionName(String competitionName) {
        if (StringUtils.isNotEmpty(competitionName))
        {
            return competitionsMapper.selectCompetitionsByCompetitionName(competitionName);
        }
        return null;
    }
}